﻿using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

/// <summary>
/// 游戏UI管理
/// </summary>
public class GameUIManager : MonoBehaviour
{
    public static GameUIManager Instance;

    [Header("关卡提示内容")]
    public Transform hintPanel;
    public Transform hintContent;
    [Header("暂停界面内容")]
    public Transform pausePanel;
    public Transform pauseContent;
    [Header("胜利界面内容")]
    public Transform winPanel;
    public Transform winTile;
    public Transform winEffs;
    public Transform winGlow;
    public Transform winBtns;
    public Text timeText;
    [Header("失败界面内容")]
    public Transform failPanel;
    public Transform failTile;
    public Transform failEffs;
    public Transform failGlow;
    public Transform failBtns;
    // 数据
    public Text levelText;
    public Text scoreText;
    public int scores;

    // 游戏是否结束
    public bool isGameOver;

    // 游戏时长
    [SerializeField]
    private float gameTime = 0;
    private int minute;
    private int second;

    private void Awake()
    {
        Instance = this;
    }

    private void Start()
    {
        if (PlayerPrefs.GetInt("Current_Level", 1) < 5)
            levelText.text = "第  " + PlayerPrefs.GetInt("Current_Level", 1) + "  关";
        else
            ShowGameHint();
        minute = second = 0;
        scores = 0;
        UpdateScores(scores);
    }

    private void Update()
    {
        if (!isGameOver)
            gameTime += Time.deltaTime;
    }

    /// <summary>
    /// 更新分数
    /// </summary>
    public void UpdateScores(int score)
    {
        DOTween.To(
        () => scores,
        x =>
        {
            scoreText.text = Mathf.Floor(x).ToString();
        },
        score,
        1f).SetEase(Ease.InOutQuint).SetUpdate(true)
        .OnComplete(() =>
        {
            scores = score;
        });
    }

    /// <summary>
    /// 游戏提示
    /// </summary>
    public void ShowGameHint()
    {
        if (PlayerPrefs.GetInt("Current_Level", 1) < 11)
            levelText.text = "第  " + PlayerPrefs.GetInt("Current_Level", 1) + "  关";
        else
            levelText.text = "每 日 一 关";
        hintPanel.localScale = hintContent.localScale = new Vector3(0, 0, 1);
        hintPanel.gameObject.SetActive(true);
        // 弹出
        DOTween.To(() => hintPanel.localScale, x => hintPanel.localScale = x, new Vector3(1, 1, 1), 0.01f).SetEase(Ease.InOutExpo).OnComplete(() =>
        {
            DOTween.To(() => hintContent.localScale, x => hintContent.localScale = x, new Vector3(1, 1, 1), 0.5f).SetEase(Ease.OutBack);
            Debug.Log("难度飙升");
        });
        // 收回
        DOTween.To(() => hintContent.localScale, x => hintContent.localScale = x, new Vector3(0, 0, 1), 0.5f).SetEase(Ease.InBack).SetDelay(1.25f).OnComplete(() =>
        {
            DOTween.To(() => hintPanel.localScale, x => hintPanel.localScale = x, new Vector3(0, 0, 1), 0.01f).SetEase(Ease.InOutExpo).OnComplete(() =>
            {
                hintPanel.gameObject.SetActive(false);
            });
        });

    }

    /// <summary>
    /// 暂停游戏
    /// </summary>
    public void PauseBtnClicked()
    {
        AudioManager.Instance.PlayButtonSound();
        pausePanel.localScale = pauseContent.localScale = new Vector3(0, 0, 1);
        pausePanel.gameObject.SetActive(true);
        DOTween.To(() => pausePanel.localScale, x => pausePanel.localScale = x, new Vector3(1, 1, 1), 0.01f).SetEase(Ease.InOutExpo).OnComplete(() =>
        {
            DOTween.To(() => pauseContent.localScale, x => pauseContent.localScale = x, new Vector3(1, 1, 1), 0.5f).SetEase(Ease.OutBack).SetUpdate(true);
            Time.timeScale = 0;
        });
    }

    /// <summary>
    /// 继续游戏
    /// </summary>
    public void ResumeBtnClicked()
    {
        AudioManager.Instance.PlayButtonSound();
        DOTween.To(() => pauseContent.localScale, x => pauseContent.localScale = x, new Vector3(0, 0, 1), 0.5f).SetEase(Ease.InBack).SetUpdate(true).OnComplete(() =>
        {
            DOTween.To(() => pausePanel.localScale, x => pausePanel.localScale = x, new Vector3(0, 0, 1), 0.01f).SetEase(Ease.InOutExpo).SetUpdate(true).OnComplete(() =>
            {
                Time.timeScale = 1;
                pausePanel.gameObject.SetActive(false);
            });
        });
    }

    /// <summary>
    /// 重新开始（暂停界面）
    /// </summary>
    public void RestartBtnClickedOnPause()
    {
        AudioManager.Instance.PlayButtonSound();
        DOTween.To(() => pauseContent.localScale, x => pauseContent.localScale = x, new Vector3(0, 0, 1), 0.5f).SetEase(Ease.InBack).SetUpdate(true).OnComplete(() =>
        {
            Time.timeScale = 1;
            SceneManager.LoadScene("Game");
        });
    }

    /// <summary>
    /// 主界面（暂停界面）
    /// </summary>
    public void HomeBtnClickedOnPause()
    {
        AudioManager.Instance.PlayButtonSound();
        DOTween.To(() => pauseContent.localScale, x => pauseContent.localScale = x, new Vector3(0, 0, 1), 0.5f).SetEase(Ease.InBack).SetUpdate(true).OnComplete(() =>
        {
            Time.timeScale = 1;
            SceneManager.LoadScene("Menu");
        });
    }

    /// <summary>
    /// 游戏胜利界面
    /// </summary>
    public void ShowGameWinPanel()
    {
        AudioManager.Instance.PlayWinSound();
        if (!isGameOver)
            isGameOver = true;
        PlayerPrefs.SetInt("Level_" + (PlayerPrefs.GetInt("Current_Level", 1) + 1), 1);
        winPanel.localScale = winTile.localScale = winEffs.localScale = winBtns.localScale = new Vector3(0, 0, 1);
        winPanel.gameObject.SetActive(true);
        DOTween.To(() => winBtns.localScale, x => winBtns.localScale = x, new Vector3(1, 1, 1), 0.25f).SetEase(Ease.OutBack).SetDelay(0.35f);
        DOTween.To(() => winPanel.localScale, x => winPanel.localScale = x, new Vector3(1, 1, 1), 0.01f).SetEase(Ease.InOutExpo).OnComplete(() =>
        {
            timeText.text = "通关用时" + GetMintue(gameTime) + "分" + GetSecond(gameTime) + "秒";
            DOTween.To(() => winTile.localScale, x => winTile.localScale = x, new Vector3(1, 1, 1), 0.5f).SetEase(Ease.OutBack).OnComplete(() =>
            {
                DOTween.To(() => winEffs.localScale, x => winEffs.localScale = x, new Vector3(1, 1, 1), 0.5f).SetEase(Ease.OutBack).OnComplete(() =>
                {
                    winGlow.DOLocalRotate(new Vector3(0, 0, 180), 2, RotateMode.FastBeyond360).SetEase(Ease.Linear).SetLoops(-1, LoopType.Incremental);
                });
            });
        });
    }

    /// <summary>
    /// 游戏失败界面
    /// </summary>
    public void ShowGameFailPanel()
    {
        AudioManager.Instance.PlayFailSound();
        if (!isGameOver)
            isGameOver = true;
        failPanel.localScale = failTile.localScale = failEffs.localScale = failBtns.localScale = new Vector3(0, 0, 1);
        failPanel.gameObject.SetActive(true);
        DOTween.To(() => failBtns.localScale, x => failBtns.localScale = x, new Vector3(1, 1, 1), 0.25f).SetEase(Ease.OutBack).SetDelay(0.35f);
        DOTween.To(() => failPanel.localScale, x => failPanel.localScale = x, new Vector3(1, 1, 1), 0.01f).SetEase(Ease.InOutExpo).SetUpdate(true).OnComplete(() =>
        {
            DOTween.To(() => failTile.localScale, x => failTile.localScale = x, new Vector3(1, 1, 1), 0.5f).SetEase(Ease.OutBack).OnComplete(() =>
            {
                DOTween.To(() => failEffs.localScale, x => failEffs.localScale = x, new Vector3(1, 1, 1), 0.5f).SetEase(Ease.OutBack).OnComplete(() =>
                {
                    failGlow.DOLocalRotate(new Vector3(0, 0, 180), 3, RotateMode.FastBeyond360).SetEase(Ease.Linear).SetLoops(-1, LoopType.Incremental);
                });
            });
        });
    }

    /// <summary>
    /// 复活，继续游戏
    /// </summary>
    public void ContinueBtnClicked()
    {
        AudioManager.Instance.PlayButtonSound();

        DOTween.To(() => failTile.localScale, x => failTile.localScale = x, new Vector3(0, 0, 1), 0.5f).SetEase(Ease.InBack);
        DOTween.To(() => failEffs.localScale, x => failEffs.localScale = x, new Vector3(0, 0, 1), 0.5f).SetEase(Ease.InBack);
        DOTween.To(() => failBtns.localScale, x => failBtns.localScale = x, new Vector3(0, 0, 1), 0.5f).SetEase(Ease.InBack).OnComplete(() =>
        {
            DOTween.To(() => failPanel.localScale, x => failPanel.localScale = x, new Vector3(0, 0, 1), 0.01f).SetEase(Ease.InOutExpo).SetUpdate(true).OnComplete(() =>
            {
                DOTween.KillAll();
                failPanel.gameObject.SetActive(false);
                isGameOver = false;
                Time.timeScale = 1;
                // 消除方块
                TileManager.Instance.ClearTileHints();
            });
        });
    }

    /// <summary>
    /// 重新开始（失败界面）
    /// </summary>
    public void RestartBtnClickedOnFail()
    {
        AudioManager.Instance.PlayButtonSound();
        DOTween.Kill(transform.name);
        DOTween.To(() => failTile.localScale, x => failTile.localScale = x, new Vector3(0, 0, 1), 0.5f).SetEase(Ease.InBack);
        DOTween.To(() => failEffs.localScale, x => failEffs.localScale = x, new Vector3(0, 0, 1), 0.5f).SetEase(Ease.InBack);
        DOTween.To(() => failBtns.localScale, x => failBtns.localScale = x, new Vector3(0, 0, 1), 0.5f).SetEase(Ease.InBack).OnComplete(() =>
        {
            DOTween.To(() => failPanel.localScale, x => failPanel.localScale = x, new Vector3(0, 0, 1), 0.01f).SetEase(Ease.InOutExpo).SetUpdate(true).OnComplete(() =>
            {
                SceneManager.LoadScene("Game");
            });
        });
    }

    /// <summary>
    /// 主界面（失败界面）
    /// </summary>
    public void HomeBtnClickedOnFail()
    {
        AudioManager.Instance.PlayButtonSound();
        DOTween.Kill(transform.name);
        DOTween.To(() => failTile.localScale, x => failTile.localScale = x, new Vector3(0, 0, 1), 0.5f).SetEase(Ease.InBack);
        DOTween.To(() => failEffs.localScale, x => failEffs.localScale = x, new Vector3(0, 0, 1), 0.5f).SetEase(Ease.InBack);
        DOTween.To(() => failBtns.localScale, x => failBtns.localScale = x, new Vector3(0, 0, 1), 0.5f).SetEase(Ease.InBack).OnComplete(() =>
        {
            DOTween.To(() => failPanel.localScale, x => failPanel.localScale = x, new Vector3(0, 0, 1), 0.01f).SetEase(Ease.InOutExpo).SetUpdate(true).OnComplete(() =>
            {
                SceneManager.LoadScene("Menu");
            });
        });
    }

    /// <summary>
    /// 重新开始（胜利界面）
    /// </summary>
    public void RestartBtnClickedOnWin()
    {
        AudioManager.Instance.PlayButtonSound();
        DOTween.To(() => winTile.localScale, x => winTile.localScale = x, new Vector3(0, 0, 1), 0.5f).SetEase(Ease.InBack);
        DOTween.To(() => winEffs.localScale, x => winEffs.localScale = x, new Vector3(0, 0, 1), 0.5f).SetEase(Ease.InBack);
        DOTween.To(() => winBtns.localScale, x => winBtns.localScale = x, new Vector3(0, 0, 1), 0.5f).SetEase(Ease.InBack).OnComplete(() =>
        {
            DOTween.To(() => winPanel.localScale, x => failPanel.localScale = x, new Vector3(0, 0, 1), 0.01f).SetEase(Ease.InOutExpo).SetUpdate(true).OnComplete(() =>
            {
                SceneManager.LoadScene("Game");
            });
        });
    }

    /// <summary>
    /// 主界面（胜利界面）
    /// </summary>
    public void HomeBtnClickedOnWin()
    {
        AudioManager.Instance.PlayButtonSound();
        DOTween.To(() => winTile.localScale, x => winTile.localScale = x, new Vector3(0, 0, 1), 0.5f).SetEase(Ease.InBack);
        DOTween.To(() => winEffs.localScale, x => winEffs.localScale = x, new Vector3(0, 0, 1), 0.5f).SetEase(Ease.InBack);
        DOTween.To(() => winBtns.localScale, x => winBtns.localScale = x, new Vector3(0, 0, 1), 0.5f).SetEase(Ease.InBack).OnComplete(() =>
        {
            DOTween.To(() => winPanel.localScale, x => failPanel.localScale = x, new Vector3(0, 0, 1), 0.01f).SetEase(Ease.InOutExpo).SetUpdate(true).OnComplete(() =>
            {
                SceneManager.LoadScene("Menu");
            });
        });
    }

    /// <summary>
    /// 下一关
    /// </summary>
    public void NextBtnClicked()
    {
        AudioManager.Instance.PlayButtonSound();
        DOTween.To(() => winTile.localScale, x => winTile.localScale = x, new Vector3(0, 0, 1), 0.5f).SetEase(Ease.InBack);
        DOTween.To(() => winEffs.localScale, x => winEffs.localScale = x, new Vector3(0, 0, 1), 0.5f).SetEase(Ease.InBack);
        DOTween.To(() => winBtns.localScale, x => winBtns.localScale = x, new Vector3(0, 0, 1), 0.5f).SetEase(Ease.InBack).OnComplete(() =>
        {
            DOTween.To(() => winPanel.localScale, x => winPanel.localScale = x, new Vector3(0, 0, 1), 0.01f).SetEase(Ease.InOutExpo).SetUpdate(true).OnComplete(() =>
            {
                if (PlayerPrefs.GetInt("Current_Level", 1) < 11)
                    PlayerPrefs.SetInt("Current_Level", PlayerPrefs.GetInt("Current_Level", 1) + 1);
                SceneManager.LoadScene("Game");
            });
        });
    }

    /// <summary>
    /// 按钮音效
    /// </summary>
    public void PlayButtonSound()
    {
        AudioManager.Instance.PlayButtonSound();
    }

    /// <summary>
    /// 获取游戏时长
    /// </summary>
    private string GetMintue(float time)
    {
        minute = (int)(time / 60);
        string timerStr;
        if (minute < 10)
            timerStr = "0" + minute.ToString();
        else
            timerStr = minute.ToString();
        return timerStr;
    }

    /// <summary>
    /// 获取游戏时长
    /// </summary>
    private string GetSecond(float time)
    {
        second = (int)(time % 60);
        string timerStr;
        if (second < 10)
            timerStr = "0" + second.ToString();
        else
            timerStr = second.ToString();
        return timerStr;
    }

}
